package com.work.saturn.account;

import com.alibaba.fastjson.JSON;
import com.immo.libcommon.base.BaseActivity;
import com.immo.libcommon.base.LoginBean;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.http.RequestSuccess;
import com.immo.libcommon.utils.SharePreferenceUtils;

import com.work.saturn.R;
import com.work.saturn.main.MainActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @content 登录页面
 * @date 2018/7/10
 */
public class LoginActivity extends BaseActivity {
    /**
     * 用户名
     */
    @BindView(R.id.login_input_username)
    EditText userName;
    /**
     * 密码
     */
    @BindView(R.id.login_input_password)
    EditText password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //禁止进入时弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        ButterKnife.bind(this);
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        String usename = userName.getText().toString();
        String ps = password.getText().toString();
        if (usename.length() == 0) {
            toast("用户名不能为空");
            return false;
        }
        if (ps.length() == 0) {
            toast("密码不能为空");
            return false;
        }
        return true;
    }


    /**
     * 登录成功，将用户名和token存储
     *
     * @param token
     */
    private void loginSuccess(String token) {
        //存储用户名和token
        SharePreferenceUtils.put(LoginActivity.this, "userName", userName.getText().toString());
        SharePreferenceUtils.put(LoginActivity.this, "token", token);
        //跳到首页
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }


    /**
     * 登录接口请求
     */
    private void LoginNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("username", userName.getText().toString());
        params.put("password", password.getText().toString());
        new NetworkRequest(new RequestSuccess() {
            @Override
            public void loadHttp(Object object, String data) {
                if (object instanceof LoginBean) {
                    LoginBean loginBean = (LoginBean) object;
                    toast("登录成功");
                    loginSuccess(loginBean.getPayload().getToken());
                }
            }
        }).requestPost(BaseUrl.LOGIN,
                this, JSON.toJSONString(params), LoginBean.class, true, false);
    }

    @OnClick({R.id.login_back, R.id.login_submit, R.id.login_to_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_back:
                //返回
                finish();
                break;
            case R.id.login_submit:
                //提交
                if (isCheck()) {
                    LoginNet();
                }
                break;
            case R.id.login_to_register:
                //跳到注册
                Intent toRegister = new Intent(this, RegisterActivity.class);
                startActivity(toRegister);
                break;
        }
    }
}
